package com.zimmur.app.mocolink.manage.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.domain.param.GroupGoodsParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsForManageVo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsInfo;
import com.zimmur.app.mocolink.domain.vo.GroupGoodsManageVo;
import com.zimmur.app.mocolink.domain.vo.GroupSceneVo;
import com.zimmur.app.mocolink.domain.vo.GroupStyleVo;
import com.zimmur.app.mocolink.manage.web.shiro.token.TokenManager;
import com.zimmur.app.mocolink.service.IGroupGoodsService;
import com.zimmur.app.mocolink.service.IGroupSceneService;
import com.zimmur.app.mocolink.service.IGroupStyleService;

@Controller
@RequestMapping("/group")
public class GroupGoodsController extends BaseController {
	@Autowired
	private IGroupGoodsService groupGoodsService;
	@Autowired
	private IGroupSceneService groupSceneService;
	@Autowired
	private IGroupStyleService groupStyleService;
	@RequestMapping("/index")
	public  String index(Model model){
		GroupGoodsParam groupGoodsParam = new GroupGoodsParam(1);
		//1.场景
		List<GroupSceneVo> sceneList=groupSceneService.getAllScene();
		//2.风格
		List<GroupStyleVo> styleList= groupStyleService.getAllStyle();
		//3.查询组货信息
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParamForManage(groupGoodsParam);
		model.addAttribute("page", page);
		model.addAttribute("sceneList", sceneList);
		model.addAttribute("styleList", styleList);
		model.addAttribute("param", groupGoodsParam);
		//model.addAttribute("chooseType", chooseType);
		return "/center/groupGoods/list";
	}
	/**
	 * （公共页面）查询组货
	 *@Description
	 *@param model
	 *@return 
	 *@author zcx
	 *@Time 2017年10月31日 下午6:05:16
	 */
	@RequestMapping("/queryGroupList")
	public  String queryGoodsList(Model model){
		Integer pageNo=getParam("pageNo", 1);
		String scene=getParam("scene");
		String style=getParam("style");
		String keyword=getParam("keyword");
		String chooseType = getParam("chooseType","one");
		GroupGoodsParam groupGoodsParam = new GroupGoodsParam();
		if(!StringUtils.isBlank(scene)){
			groupGoodsParam.setScene(scene);
		}
		if(!StringUtils.isBlank(style)){
			groupGoodsParam.setStyle(style);
		}
		if(!StringUtils.isBlank(keyword)){
			groupGoodsParam.setKeyword(keyword);
		}
		groupGoodsParam.setPageNo(pageNo);
		List<GroupSceneVo> sceneList=groupSceneService.getAllScene();
		List<GroupStyleVo> styleList= groupStyleService.getAllStyle();
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParamForManage(groupGoodsParam);
		model.addAttribute("page", page);
		model.addAttribute("sceneList", sceneList);
		model.addAttribute("styleList", styleList);
		model.addAttribute("param", groupGoodsParam);
		model.addAttribute("chooseType", chooseType);
		return "/center/public/groupList";
	}
	/**
	* <p>Title: getGroupGoodsList</p>
	* <p>Description: 分页查询组货信息——后台管理</p>
	* @param model
	* @return
	 */
	@RequestMapping("/getGroupGoodsList")
	public  String getGroupGoodsList(Model model){
		Integer pageNo=getParam("pageNo", 1);
		Integer pageSize=getParam("pageSize", 10);
		String scene=getParam("scene");
		String style=getParam("style");
		String keyword=getParam("keyword");
		Integer isRecommend=getParam("isRecommend",-1);
		GroupGoodsParam groupGoodsParam = new GroupGoodsParam();
		if(!StringUtils.isBlank(scene)){
			groupGoodsParam.setScene(scene);
		}
		if(!StringUtils.isBlank(style)){
			groupGoodsParam.setStyle(style);
		}
		if(!StringUtils.isEmpty(keyword)){
			groupGoodsParam.setKeyword(keyword);
		}
		if(isRecommend>=0){
			groupGoodsParam.setIsRecommend(isRecommend);
		}
		groupGoodsParam.setPageNo(pageNo);
		groupGoodsParam.setPageSize(pageSize);
		//1.场景
		List<GroupSceneVo> sceneList=groupSceneService.getAllScene();
		//2.风格
		List<GroupStyleVo> styleList= groupStyleService.getAllStyle();
		//3.查询组货信息
		PageUtil<GroupGoodsInfo> page = groupGoodsService.queryInfoByParamForManage(groupGoodsParam);
		model.addAttribute("page", page);
		model.addAttribute("sceneList", sceneList);
		model.addAttribute("styleList", styleList);
		model.addAttribute("param", groupGoodsParam);
		//model.addAttribute("chooseType", chooseType);
		return "/center/groupGoods/list";
	}
	/**
	* <p>Title: edit</p>
	* <p>Description: 初始化组货编辑页面</p>
	* @param model
	* @return
	 */
	@RequestMapping("/edit")
	public  String edit(Model model){
		Integer groupId  = getParam("groupId",0);
		//1.场景
		List<GroupSceneVo> sceneList=groupSceneService.getAllScene();
		//2.风格
		List<GroupStyleVo> styleList= groupStyleService.getAllStyle();
		//3.组货信息
		GroupGoodsForManageVo vo =null;
		if(groupId == 0){//新增初始化编辑页面
			vo = new GroupGoodsForManageVo();
		}else{//修改商品信息初始化编辑页面
			vo = groupGoodsService.queryGroupGoodsInfo(groupId);
			String scene = vo.getScene();
			String style = vo.getStyle();
			if(!StringUtils.isBlank(scene)){
				String[] sceneArray = scene.split(",");
				Map<String,GroupSceneVo> sceneMap = new HashMap<>();
				for(GroupSceneVo sceneVo:sceneList){
					for(GroupSceneVo childSceneVo:sceneVo.getChildrenList()){
						childSceneVo.setChildrenList(null);
						sceneMap.put(childSceneVo.getName(), childSceneVo);
					}
				}
				for(String sceneStr:sceneArray){
					GroupSceneVo groupSceneVo = sceneMap.get(sceneStr);
					if(groupSceneVo!=null){
						groupSceneVo.setChildrenList(new ArrayList<GroupSceneVo>());
					}
				}
			}
			if(!StringUtils.isBlank(style)){
				String[] styleArray = style.split(",");
				Map<String,GroupStyleVo> styleMap = new HashMap<>();
				for(GroupStyleVo styleVo:styleList){
					for(GroupStyleVo childStyleVo:styleVo.getChildrenList()){
						childStyleVo.setChildrenList(null);
						styleMap.put(childStyleVo.getName(), childStyleVo);
					}
				}
				for(String styleStr:styleArray){
					GroupStyleVo groupStyleVo = styleMap.get(styleStr);
					if(groupStyleVo!=null){
						groupStyleVo.setChildrenList(new ArrayList<GroupStyleVo>());
					}
				}
			}
		}
		model.addAttribute("sceneList", sceneList);
		model.addAttribute("styleList", styleList);
		model.addAttribute("group", vo);
		return "/center/groupGoods/edit";
	}
	/**
	* <p>Title: deleteGroupGoods</p>
	* <p>Description: 删除组货信息</p>
	* @param model
	* @return
	 */
	@ResponseBody
	@RequestMapping("/deleteGroupGoods")
	public  Map<String,String> deleteGroupGoods(Model model){
		Map<String,String> map=new HashMap<String, String>();
		Integer groupId= getParam("groupId", 0);
		Integer flag = groupGoodsService.deleteGroupGoods(groupId);
		if(flag>0){
			map.put("code","000000");
			map.put("msg", "操作成功");
		}else{
			map.put("code","100000");
			map.put("msg", "操作失败");
		}
		return map;
	}
	@ResponseBody
	@RequestMapping("/saveGroupGoods")
	public  Map<String,String> saveGroupGoods(Model model){
		Map<String,String> map=new HashMap<String, String>();
	    String groupGoodsVo=getParam("groupGoodsVo");
		String userName = TokenManager.getNickname(); 
		GroupGoodsManageVo vo = JSONObject.parseObject(groupGoodsVo, GroupGoodsManageVo.class);
	    int flag=0;
	    Integer groupId = vo.getGroupGoodsId();
	    if(groupId!=null&&groupId>0){//修改商品信息保存数据
	    	flag = groupGoodsService.updateGroupGoodsForManage(vo,userName);
	    }else{//新增商品信息保存数据
	    	flag = groupGoodsService.saveGroupGoodsForManage(vo,userName);
	    }
		if(flag>0){
			map.put("code","000000");
			map.put("msg", "操作成功");
		}else{
			map.put("code","100000");
			map.put("msg", "操作失败");
		}
		return map;
	}
}
